package org.example.orders.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.orders.entity.Orders;
import org.example.orders.service.OrdersService;
import org.example.orders.vo.R;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.net.URLDecoder;
import java.util.List;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2025-08-06 21:29:24
 */
@RestController
@RequestMapping("orders")
public class OrdersController  {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;


    @GetMapping
    @SentinelResource(value = "all",fallback = "doMyFallback",blockHandler = "doZuSeJiangJi")
    public R selectAll(int a) throws InterruptedException {
     //这个参数 a 作为人为模拟异常的依据,当a为1的时候,认为查询较慢,为2的时候,抛异常
        if(a == 1){
            Thread.sleep(3000);
        } else if (a == 2) {
            throw new RuntimeException("order类的selectAll方法接收到的参数为2,人为抛得异常....");
        }

        return R.success(this.ordersService.list());
    }
    // 当上面的  selectAll方法执行期间,发生了 sentinel专用异常的时候,会自动执行这个降级的逻辑!
    public R doZuSeJiangJi(int a, BlockException e) throws InterruptedException {
        System.out.println("监控到  selectAll 方法被sentinel制裁了.....异常信息是:");
        e.printStackTrace();
        //return R.error("亲,别慌,刷新试试...");
        if (e instanceof AuthorityException) {
            // 授权异常：返回“没有登录”
            return R.error("没有登录");
        } else {
            // 其他阻塞异常（如流控、降级）：返回原有提示
            return R.error("亲,别慌,刷新试试...");
        }
    }

    // 当上面的  selectAll方法执行期间,发生了非 sentinel规定的异常的时候,会自动执行这个降级的逻辑!
    public R doMyFallback(int a,Throwable e) throws InterruptedException {
        System.out.println("监控到  selectAll 方法出异常了.....异常信息是:");
        e.printStackTrace();
        return R.error("亲,运气爆棚了,碰上服务器升级了...");

    }

        @PostMapping
    public R selectAll(HttpServletRequest request) {
        String s = request.getHeader("loginuser");
        String decode = URLDecoder.decode(s);
        System.out.println("查询全部订单获取的令牌"+ decode+"----------------------------------------");



        return R.success(ordersService.listAndUser());
    }


    @GetMapping("h/{id}")
    @SentinelResource(value = "h",blockHandler = "hhhh")
    public R selectOne1(@PathVariable Serializable id) {

        Orders byIdAndUser = this.ordersService.getByIdAndUser(id);
        return R.success(byIdAndUser);

    }
    public R hhhh(@PathVariable Serializable id,BlockException e) {
        System.out.println("监控到  selectAll 方法被sentinel制裁了.....异常信息是:");
        e.printStackTrace();
       return R.error("亲,运气爆棚了,碰上服务器升级了hhhhh");
    }

    @GetMapping("{id}")
    @SentinelResource("myid")
    public R selectOne(@PathVariable Serializable id) {

        Orders byIdAndUser = this.ordersService.getByIdAndUser(id);
        return R.success(byIdAndUser);

    }



}

